package com.uwlaser.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.CraftParam;
import com.uwlaser.domain.CraftParamDetail;
import com.uwlaser.service.CraftParamService;
import com.uwlaser.service.dto.CraftParamDto;
import com.uwlaser.service.dto.CraftParamQueryParam;
import com.uwlaser.service.mapper.CraftParamDetailMapper;
import com.uwlaser.service.mapper.CraftParamMapper;
import com.uwlaser.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工艺参数集合管理")
@RequestMapping("/api/craftParam")
public class CraftParamController {

    private final CraftParamService craftParamService;

    private final CraftParamDetailMapper craftParamDetailMapper;

    @GetMapping
    @Log("查询工艺参数集合")
    @ApiOperation("查询工艺参数集合")
    @PreAuthorize("@el.check('craftParam:list')")
    public ResponseEntity query(CraftParamQueryParam query, Pageable pageable){
        return new ResponseEntity<>(craftParamService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("getById")
    @Log("根据ID查询参数集")
    @ApiOperation("根据ID查询参数集")
    @PreAuthorize("@el.check('craftParam:list')")
    public ResponseEntity getById(Integer id){
        return new ResponseEntity<>(craftParamService.getById(id),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工艺参数集合")
    @ApiOperation("新增工艺参数集合")
    @PreAuthorize("@el.check('craftParam:add')")
    public ResponseEntity create(@Validated @RequestBody CraftParamDto resources){
        return new ResponseEntity<>(craftParamService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工艺参数集合")
    @ApiOperation("修改工艺参数集合")
    @PreAuthorize("@el.check('craftParam:edit')")
    public ResponseEntity update(@Validated @RequestBody CraftParamDto resources){
        craftParamService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工艺参数集合")
    @ApiOperation("删除工艺参数集合")
    @PreAuthorize("@el.check('craftParam:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        for (Integer id : ids) {
            CraftParamDetail craftParamDetail = new CraftParamDetail();
            craftParamDetail.setParamsId(id);
            craftParamDetailMapper.delete(new QueryWrapper<>(craftParamDetail));
        }
        craftParamService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getCraftParamId")
    @Log("根据产品型号查询工艺参数")
    @ApiOperation("根据产品型号查询工艺参数")
    public ResponseEntity getCraftParamId(CraftParamQueryParam query){
        return new ResponseEntity<>(craftParamService.getCraftParamId(query.getProductCode()),HttpStatus.OK);
    }

    /*
    @Log("导出工艺参数集合")
    @ApiOperation("导出工艺参数集合")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('craftParam:list')")
    public void download(HttpServletResponse response, CraftParamQueryParam query) throws IOException {
        craftParamService.download(craftParamService.queryAll(query), response);
    }*/

}
